﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 游戏规则
/// </summary>
public class Rules
{
    /// <summary>
    /// 是否是单
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isSingle(List<Card> cards)
    {
        return cards.Count == 1;
    }
    /// <summary>
    /// 是否是对
    /// </summary>
    /// <param name="cards">选择的手牌<param>
    /// <returns></returns>
    public static bool isDouble(List<Card> cards)
    {
        if (cards.Count == 2)
        {
            if (cards[0].CardWeight == cards[1].CardWeight)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    /// <summary>
    /// 是否是顺子
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isStraight(List<Card> cards)
    {
        if (cards.Count < 5 || cards.Count > 12)
            return false;

        for (int i = 0; i < cards.Count - 1; i++)
        {
            if (cards[i].CardWeight != cards[i + 1].CardWeight - 1)           
                return false;
            
            if (cards[i].CardWeight > Weight.One || cards[i + 1].CardWeight > Weight.One)            
                return false;            
        }
        return true;
    }
    /// <summary>
    /// 是否是双顺
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isDobleStraight(List<Card> cards)
    {
        if (cards.Count < 6 || cards.Count %2 != 0)//牌的数量必须为偶数
            return false;
        for (int i = 0; i < cards.Count-2;  i += 2)
        {
            if (cards[i].CardWeight != cards[i + 1].CardWeight)
                return false;
            if (Mathf.Abs((int)cards[i].CardWeight - (int)cards[i + 2].CardWeight) != 1)
                return false;
            if (cards[i].CardWeight > Weight.One || cards[i + 2].CardWeight > Weight.One)
                return false;
        }
        return true;
    }
    /// <summary>
    /// 是否是三顺飞机
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isTripleStraight(List<Card> cards)
    {
        if (cards.Count < 6 || cards.Count % 3 != 0)//牌的数量必须为3的倍数
            return false;
        for (int i = 0; i < cards.Count-3; i += 3)
        {
            if (cards[i].CardWeight != cards[i + 1].CardWeight|| cards[i].CardWeight != cards[i + 2].CardWeight)
                return false;
            if (Mathf.Abs((int)cards[i].CardWeight - (int)cards[i + 3].CardWeight) != 1)
                return false;
            if (cards[i].CardWeight > Weight.One || cards[i + 3].CardWeight > Weight.One)
                return false;
        }
        return true;
    }
    /// <summary>
    /// 是否是三不带
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isThree(List<Card> cards)
    {
        if (cards.Count != 3)
            return false;
        if (cards[0].CardWeight != cards[1].CardWeight || cards[0].CardWeight != cards[2].CardWeight)
            return false;
        return true;
    }
    /// <summary>
    /// 是否是三带一
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isThreeAndOne(List<Card> cards)
    {
        if (cards.Count != 4)
            return false;
        if (cards[0].CardWeight == cards[1].CardWeight && cards[1].CardWeight == cards[2].CardWeight)
            return true;
        if (cards[1].CardWeight == cards[2].CardWeight && cards[2].CardWeight == cards[3].CardWeight)
            return true;
        return false;
    }
    /// <summary>
    /// 是否是三带二
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isThreeAndTow(List<Card> cards)
    {
        if (cards.Count != 5)
            return false;
        if (cards[0].CardWeight == cards[1].CardWeight && cards[1].CardWeight == cards[2].CardWeight&&cards[3].CardWeight==cards[4].CardWeight)
            return true;
        if (cards[0].CardWeight == cards[1].CardWeight && cards[2].CardWeight == cards[3].CardWeight && cards[3].CardWeight == cards[4].CardWeight)
            return true;
        return false;
    }
    /// <summary>
    /// 是否是炸弹
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isBoom(List<Card> cards)
    {
        if (cards.Count != 4)
            return false;
        if (cards[0].CardWeight == cards[1].CardWeight && cards[1].CardWeight == cards[2].CardWeight && cards[2].CardWeight == cards[3].CardWeight)
            return true;
        return false;
    }
    /// <summary>
    /// 是否是王炸
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <returns></returns>
    public static bool isJokerBoom(List<Card> cards)
    {
        if (cards.Count != 2)
            return false;
        if (cards[0].CardWeight == Weight.LJoker && cards[1].CardWeight == Weight.SJoker)
            return true;
        if (cards[1].CardWeight == Weight.LJoker && cards[0].CardWeight == Weight.SJoker)
            return true;
        return false;
    }
    /// <summary>
    /// 是否可以出牌
    /// </summary>
    /// <param name="cards">选择的手牌</param>
    /// <param name="cardtype">出牌的类型</param>
    /// <returns></returns>
    public static bool isCanPop(List<Card> cards, out CardType cardtype)
    {
        CardType type = CardType.None;
        bool iscan = false;
        switch (cards.Count)
        {
            case 1:
                if (isSingle(cards))
                {
                    type = CardType.Single;
                    iscan = true;
                }
                break;
            case 2:
                if (isDouble(cards))
                {
                    type = CardType.Double;
                    iscan = true;
                }
                else if (isJokerBoom(cards))
                {
                    type = CardType.JokerBoom;
                    iscan = true;
                }
                break;
            case 3:
                if (isThree(cards))
                {
                    type = CardType.Three;
                    iscan = true;
                }
                break;
            case 4:
                if (isBoom(cards))
                {
                    type = CardType.Boom;
                    iscan = true;
                }
                else if (isThreeAndOne(cards))
                {
                    type = CardType.ThreeAndOne;
                    iscan = true;
                }
                break;
            case 5:
                if (isStraight(cards))
                {
                    type = CardType.Straight;
                    iscan = true;
                }
                else if (isThreeAndTow(cards))
                {
                    type = CardType.ThreeAndTwo;
                    iscan = true;
                }
                break;
            case 6:
                if (isStraight(cards))
                {
                    type = CardType.Straight;
                    iscan = true;
                }
                else if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                else if (isTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    iscan = true;
                }
                break;
            case 7:
                if (isStraight(cards))
                {
                    type = CardType.Straight;
                    iscan = true;
                }
                break;
            case 8:
                if (isStraight(cards))
                {
                    type = CardType.Straight;
                    iscan = true;
                }
                else if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                break;
            case 9:
                if (isStraight(cards))
                {
                    type = CardType.Straight;
                    iscan = true;
                }
                else if (isTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    iscan = true;
                }
                break;
            case 10:
                if (isStraight(cards))
                {
                    type = CardType.Straight;
                    iscan = true;
                }
                else if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                break;
            case 11:
                if (isStraight(cards))
                {
                    type = CardType.Straight;
                    iscan = true;
                }
                break;
            case 12:
                if (isStraight(cards))
                {
                    type = CardType.Straight;
                    iscan = true;
                }
                else if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                else if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                break;
            case 13:
                break;
            case 14:
                if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                break;
            case 15:
                if (isTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    iscan = true;
                }
                break;
            case 16:
                if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                break;
            case 17:
                break;
            case 18:
                if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                else if (isTripleStraight(cards))
                {
                    type = CardType.TripleStraight;
                    iscan = true;
                }
                break;
            case 19:
                break;
            case 20:
                if (isDobleStraight(cards))
                {
                    type = CardType.DoubleStraight;
                    iscan = true;
                }
                break;
            default:
                break;
        }
        cardtype = type;//将最后的类型传出去
        return iscan;
    }
}
